// 该模块是处理计算任务的 , 网络计算器

#pragma once
#include "Protocol.hpp"

class Cal
{
public:

    // 计算完要把结果返回 , 结果就是 Response 的结构化数据
    Response Execute(Request &req)
    {
        Response resp(0, 0); // code: 0表示成功
        switch (req.GetOper())
        {
        case '+':
            resp.SetResult(req.GetX() + req.GetY());
            break;
        case '-':
            resp.SetResult(req.GetX() - req.GetY());

            break;
        case '*':
            resp.SetResult(req.GetX() * req.GetY());
            break;
        case '/':
        {
            if (req.GetY() == 0)
            {
                resp.SetCode(1); // 1除零错误
            }
            else
            {
                resp.SetResult(req.GetX() / req.GetY());
            }
        }
        break;

        case '%':
        {
            if (req.GetY() == 0)
            {
                resp.SetCode(2); // 2 mod 0 错误
            }
            else
            {
                resp.SetResult(req.GetX() % req.GetY());
            }
        }
        break;

        default:
            resp.SetCode(3); // 非法操作
            break;
        }

        return resp;
    }
};